import pygame
import collidet
import math

class bt_laser(pygame.sprite.Sprite):
    
    def __init__(self, belong_to, start_degree, end_degree):
        super(bt_laser, self).__init__(self.containers)
        self.belong_to = belong_to.belong_to
        self._belong_to = belong_to
        self.original_image = pygame.image.load("data/images/bt_laser.png")
        self.image = None
        self.rect = None
        self.start_degree = -start_degree
        self.end_degree = -end_degree
        self.current_degree = self.start_degree
        self.turn_intv = 10
        self.out_intv = 10
        self.out = False
        self.power = 2
        
        if end_degree <= start_degree :
            self.degree_step = 1
        else:
            self.degree_step = -1
        
        self.rotate()
        
    def update(self):
#        print "current_degree: ", self.current_degree
        if self.current_degree == self.end_degree:
            if self.out_intv == 0:
                self.light_out()
                return
            else:
#                print "light!"
                self.out_intv -= 1
        else:
            if self.turn_intv == 0 :
                self.current_degree += self.degree_step
            else:
                self.turn_intv -= 1
        self.rotate()

        length = math.sqrt((self.image.get_width() / 2.0) ** 2\
        + (self.image.get_height() / 2.0) ** 2)
        offx, offy = collidet.get_offsets(length, self.current_degree)
        self.rect.center = (self._belong_to.rect.centerx - offx, self._belong_to.rect.centery + offy)
        
    
    def rotate(self):
        self.image = pygame.transform.rotozoom(self.original_image, self.current_degree, 1.0)
        self.rect = self.image.get_rect()
        

    def draw(self, screen):
        screen.blit(self.image, self.rect)
 

    def light_out(self):
#        print "out!"
        self.out = True
        self.kill()

class bt_laser_enemy(pygame.sprite.Sprite):
    
    def __init__(self, belong_to, start_degree, end_degree):
        super(bt_laser_enemy, self).__init__(self.containers)
        self._belong_to = belong_to
        self.original_image = pygame.image.load("data/images/bt_laser_enemy.png")
        self.image = None
        self.rect = None
        self.start_degree = -start_degree
        self.end_degree = -end_degree
        self.current_degree = self.start_degree
        self.turn_intv = 10
        self.out_intv = 10
        self.out = False
        self.power = 2
        
        if end_degree <= start_degree :
            self.degree_step = 1
        else:
            self.degree_step = -1
        
        self.rotate()
        
    def update(self):
#        print "current_degree: ", self.current_degree
        if self.current_degree == self.end_degree:
            if self.out_intv == 0:
                self.light_out()
                return
            else:
#                print "light!"
                self.out_intv -= 1
        else:
            if self.turn_intv == 0 :
                self.current_degree += self.degree_step
            else:
                self.turn_intv -= 1
        self.rotate()

        length = math.sqrt((self.image.get_width() / 2.0) ** 2\
        + (self.image.get_height() / 2.0) ** 2)
        offx, offy = collidet.get_offsets(length, self.current_degree)
        self.rect.center = (self._belong_to.rect.centerx - offx, self._belong_to.rect.centery + offy)
        
    
    def rotate(self):
        self.image = pygame.transform.rotozoom(self.original_image, self.current_degree, 1.0)
        self.rect = self.image.get_rect()
        

    def draw(self, screen):
        screen.blit(self.image, self.rect)
 

    def light_out(self):
#        print "out!"
        self.out = True
        self.kill()


class bt_laser_gun(pygame.sprite.Sprite):
    
    def __init__(self, belong_to, degree, speed):
        super(bt_laser_gun, self).__init__(self.containers)
        self.degree = degree
        self.original_image = pygame.image.load("data/images/bt_laser_gun.png")
        self.image = self.original_image
        self.forewoard_step = 5
        self.back_step = 5
        self.speed_x,self.speed_y = collidet.get_offsets(speed, degree)
        self.belong_to = belong_to
        self.laser1 = None
        self.laser2 = None
        self.rect = None
        self.not_fire = True
        self.off_set_x = 0
        self.off_set_y = 0
        self.on_fire = True
        
        self.rotate()
        
    def action(self):
        if self.on_fire:
            if self.forewoard_step :
                self.off_set_x += self.speed_x
                self.off_set_y += self.speed_y
                self.forewoard_step -= 1
            else:
                if self.not_fire:
                    self.laser1 = bt_laser(self, self.degree, self.degree + 120.0)
                    self.laser2 = bt_laser(self, self.degree, self.degree - 120.0)
                    self.not_fire = False
        else:
            if self.back_step :
                self.off_set_x -= self.speed_x
                self.off_set_y -= self.speed_y
                self.back_step -= 1
            else:
                self.kill()

        self.rect.center = (self.belong_to.rect.centerx + self.off_set_x,\
                            self.belong_to.rect.centery + self.off_set_y)
        
            
    def rotate(self):
        self.image = pygame.transform.rotozoom(self.original_image, -self.degree, 1.0)
        self.rect = self.image.get_rect()
        self.rect.center = (self.belong_to.rect.centerx, self.belong_to.rect.centery)
            
    def update(self):
#        print "in update"
        self.action()
        if not self.not_fire :
            if self.laser1 and self.laser1.out :
                self.laser1 = None
            if self.laser2 and self.laser2.out :
                self.laser2 = None
            
            if self.laser1 is None and self.laser2 is None:
#                print "Kill gun!"
#                self.kill()
                self.on_fire = False
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)